import lodash from 'lodash'
import plugin from '../../../lib/plugins/plugin.js'
import shangwufood from '../data/shanwu.js'
import zhongwufood from '../data/zhonwu.js'
import wanshangfood from '../data/wanshang.js'
import xiaoyefood from '../data/yexiao.js'

export class what2eat extends plugin {
  constructor () {
    super({
      name: 'what2eat',
      dsc: '今天吃什么',
      event: 'message',
      priority: 5000,
      rule: [
        {
          reg: '^#?添加食物',
          fnc: 'addFood'
        },
        {
          reg: '^#?删除食物',
          fnc: 'deleteFood'
        },
        {
          reg: '#?咱?(今天|明天)?(早上|早餐|早饭)吃(什么|啥|点啥)',
          fnc: 'shangwu'
        },
        {
          reg: '#?咱?(今天|明天)?(中午|午餐|午饭)吃(什么|啥|点啥)',
          fnc: 'zhongwu'
        },
        {
          reg: '#?咱?(今天|明天)?(晚上|晚餐|晚饭)吃(什么|啥|点啥)',
          fnc: 'wanshang'
        },
        {
          reg: '#?咱?(今天|明天)?(宵夜|夜宵)吃(什么|啥|点啥)',
          fnc: 'xiaoye'
        },
      ]
    })
  }

  getKey () {
    return `Yz:what2eat:foods:${this.e.group_id}`
  }

  async addFood () {
    if (!this.e.isGroup) {
      return await this.reply('请群聊发送')
    }
    const key = this.getKey()
    const foods = this.e.msg.split(' ').filter(Boolean).slice(1)
    foods.forEach(async (food) => {
      await redis.sAdd(key, food)
    })
    await this.reply(`添加了${foods.length}个群特色食物...`)
  }

  async deleteFood () {
    if (!this.e.isGroup) {
      return await this.reply('请群聊发送')
    }
    const key = this.getKey()
    const foods = this.e.msg.split(' ').filter(Boolean).slice(1)
    foods.forEach(async (food) => {
      await redis.sRem(key, food)
    })
    await this.reply(`已经尝试删除${foods.length}个群特色食物...`)
  }

  async shangwu () {
    let food = shangwufood
    if (this.e.isGroup) {
      const key = this.getKey()
      const groupFood = await redis.sMembers(key)
      food = this.e.msg.split(' ')[0]?.includes('咱')
        ? groupFood
        : [...shangwufood, ...groupFood]
    }

    if (!food || food.length == 0) return

    const result = lodash.sampleSize(food, 5).join('|')
    await this.reply(`🌟推荐尝试：${result}`, false, { at: true })
  }

  async zhongwu () {
    let food = zhongwufood
    if (this.e.isGroup) {
      const key = this.getKey()
      const groupFood = await redis.sMembers(key)
      food = this.e.msg.split(' ')[0]?.includes('咱')
        ? groupFood
        : [...zhongwufood, ...groupFood]
    }

    if (!food || food.length == 0) return

    const result = lodash.sampleSize(food, 5).join('|')
    await this.reply(`🌟推荐尝试：${result}`, false, { at: true })
  }

  async wanshang () {
    let food = wanshangfood
    if (this.e.isGroup) {
      const key = this.getKey()
      const groupFood = await redis.sMembers(key)
      food = this.e.msg.split(' ')[0]?.includes('咱')
        ? groupFood
        : [...wanshangfood, ...groupFood]
    }

    if (!food || food.length == 0) return

    const result = lodash.sampleSize(food, 5).join('|')
    await this.reply(`🌟推荐尝试：${result}`, false, { at: true })
  }

  async xiaoye () {
    let food = xiaoyefood
    if (this.e.isGroup) {
      const key = this.getKey()
      const groupFood = await redis.sMembers(key)
      food = this.e.msg.split(' ')[0]?.includes('咱')
        ? groupFood
        : [...xiaoyefood, ...groupFood]
    }

    if (!food || food.length == 0) return

    const result = lodash.sampleSize(food, 5).join('|')
    await this.reply(`🌟推荐尝试：${result}`, false, { at: true })
  }
}
